package com.yowasa.xiuxian.engine.message.activity;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.entity.po.XiuxianRedPacket;
import com.yowasa.xiuxian.mapper.XiuxianRedPacketMapper;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@CMD(name = "/抢红包")
public class SnatchingPacketHandler extends CmdMessageHandler {
    public static final Logger logger = LoggerFactory.getLogger(SnatchingPacketHandler.class);
    @Autowired
    private MessageHelper messageHelper;

    @Autowired
    private UserHelper userHelper;

    @Autowired
    private XiuxianRedPacketMapper xiuxianRedPacketMapper;

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public void execute(SendMessage message) {
        UserBO userBO = userHelper.checkOpUser(message);
        List<String> params = getAllParam(message);//获取名称
        String s = params.get(0);
        if (StringUtils.isEmpty(s)) {
            messageHelper.replyMys(message, "需要在抢红包后追加口令，普通红包需要通过按钮抢");
            return;
        }
        logger.info("口令为：{}",s);
        List<XiuxianRedPacket> packetList = xiuxianRedPacketMapper.selectList(new LambdaQueryWrapper<XiuxianRedPacket>().eq(XiuxianRedPacket::getMsg, s).eq(XiuxianRedPacket::getType, 2));
        logger.info("查询出来的红包发起列表：{}",packetList);

        packetList.forEach(e -> {
            String packet_id = e.getPacketId();

            //先判断有没有抢过
            List<XiuxianRedPacket> listTest = xiuxianRedPacketMapper.selectList(new LambdaQueryWrapper<XiuxianRedPacket>()
                    .eq(XiuxianRedPacket::getType, 0)
                    .eq(XiuxianRedPacket::getPacketId, packet_id)
                    .eq(XiuxianRedPacket::getUserId,userBO.getUserId()));
            logger.info("判断是否抢过的结果：{}",listTest);
            if(listTest.size() == 0){
                XiuxianRedPacket one = xiuxianRedPacketMapper.selectOne(new LambdaQueryWrapper<XiuxianRedPacket>()
                        .eq(XiuxianRedPacket::getType,0)
                        .eq(XiuxianRedPacket::getPacketId,packet_id)
                        .isNull(XiuxianRedPacket::getUserId)
                        .last("limit 1 for update"));
                logger.info("抢到的红包：{}",one);
                if(ObjectUtil.isNotNull(one)){
                    one.setUserId(userBO.getUserId());
                    one.setCreateTime(new Date());

                    UserBO byUserId = userHelper.getByUserId(e.getUserId());
                    xiuxianRedPacketMapper.updateById(one);
                    userBO.getUserConfig().setGold(userBO.getUserConfig().getGold() + one.getGold());
                    userHelper.saveUserInfo(userBO);
                    messageHelper.replyMys(message,userBO.getName()+"领到了"+byUserId.getName()+"发的红包，获得了"+one.getGold()+"摩拉");
                }
            }
        });

    }



    public String desc() {
        return "抢红包";
    }


}
